//
//  LoopWelcomeViewController.swift
//  LoopWeibo
//
//  Created by loop安 on 2016/12/10.
//  Copyright © 2016年 loop. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
private let bottomMargin:CGFloat = 120

class LoopWelcomeViewController: UIViewController {

    

    
    //在该方法中调用动画方法,避免动画出现bug
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        setupUI()
    
    }

    private func setupUI(){
   
        view.addSubview(icon)
        view.addSubview(lblWelcome)
        
        //剪切图片框
        icon.layer.cornerRadius = 42.5
        icon.clipsToBounds = true
        icon.sd_setImage(with: LoopUserAccountViewModel.shared.headUrl)
        
        //设置lbl的透明度
        lblWelcome.alpha = 0
        
        icon.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(bottomMargin)
            make.width.height.equalTo(85)
        }
        
        lblWelcome.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(icon.snp.bottom).offset(20)
        }
    
    }
    
    private func startAnimation(){
        let offSet = -(UIScreen.main.bounds.height - bottomMargin - icon.bounds.height)
        
        icon.snp.updateConstraints { (make) in
            make.bottom.equalTo(self.view).offset(offSet)
        }
        
        UIView.animate(withDuration: 1,
                       delay: 0,
                       usingSpringWithDamping: 0.8,
                       initialSpringVelocity: 0,
                       options: .curveLinear,
                       animations: {
                        self.view.layoutIfNeeded()
        },
                       completion: { (_) in
                        UIView.animate(withDuration: 1, animations: {
                            self.lblWelcome.alpha = 1
                        }, completion: { (_) in
                            //动画完成切换根视图控制器
//                           UIApplication.shared.keyWindow?.rootViewController = LoopTabBarController()
                            NotificationCenter.default.post(name: Notification.Name(kChangeRootController), object: nil)
                        })
        })
    }
    
    
    private lazy var icon = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    private lazy var lblWelcome = UILabel(title: "欢迎回来", fontsize: 16, textColor: #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1))
}
